import BulletCtr, { BulletInfo } from "./BulletCtr";
import { bulletManager } from "./BulletManager";
import FishCtr from "./FishCtr";
import { fishManager } from "./FishManager";
import { colliderObjManager } from "./NewQuadtree/ColliderObjManager";
import { player } from "./playerCtr";
import MyLog, { MYLOG_TYPE } from "./utils/MyLog";
import VMLabel from "./utils/VMLabel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    // onLoad () {}
    battery: cc.Node = null;
    bulletContent: cc.Node = null;
    fishContent: cc.Node = null;
    shootAnim: cc.Animation = null;

    start() {
        //  cc.assetManager.assets.count 
        //  cc.assetManager.assets.clear()
        let self = this
        cc.find("Canvas/game/touchArea").on('touchstart', this.shoot, this);
        cc.find("Canvas/game/touchArea").on('touchmove', this.moveCallback, this);

        this.shootAnim = cc.find("Canvas/game/playerContent/battery").getComponent(cc.Animation)
        this.bulletContent = cc.find('Canvas/game/bulletContent')
        this.fishContent = cc.find('Canvas/game/fishContent')
        this.battery = cc.find("Canvas/game/playerContent/battery")

        colliderObjManager.init(this.node)
        var coin = cc.find('Canvas/game/playerContent/tv_coin').getComponent(VMLabel)
        coin.setObserveValue(player, 'coin', (newValue, oldValue) => {

            let action = cc.scaleTo(0.1, 1.5)
            let sequence = cc.sequence(action,  cc.callFunc(() => {
                coin.string = newValue;
                let action = cc.scaleTo(0.2, 1)
                coin.node.runAction(action)
            }))
            coin.node.runAction(sequence)
        })

        this.autoCreateFish()
    }

    autoCreateFish() {
        let self = this
        let id = 0
        this.schedule(async () => {
            fishManager.getFishByType('prefab/fish_1')
            let fish = await fishManager.getFishByType('prefab/fish_1')
            fish.parent = self.fishContent
            fish.getComponent(FishCtr).initFish(fishManager.getFishPath(), 15, id)
            colliderObjManager.setFishCollider(fish)
            id += 1
        }, 0.3)
    }

    shoot(event) {
        console.log("dianji");
        this.shootAnim.play()
        let local = event.getLocation()
        local.x -= cc.winSize.width / 2;
        local.y -= cc.winSize.height / 2;
        if(this.battery.position.y > local.y){//开炮角度范围做一个限制
            return
        }
        this.create_bullet(this.changeAngle(this.battery.position, local))
    }

    changeAngle(p1, local) {
        // if (p1.y > local.y) return this.battery.angle
        var o = p1.x - local.x;
        var a = p1.y - local.y;
        var angle = Math.atan(o / a) * 180 / Math.PI;
        this.battery.angle = -angle;
        cc.find("battery", this.battery.parent).angle = -angle
        return angle
    }

    moveCallback(event) {
        // this.changeAngle(this.battery.position, event)
    }

    async create_bullet(angle) {
        let bullet = await bulletManager.getBullet('prefab/bullet')
        bullet.parent = this.bulletContent
        let bulletInfo: BulletInfo = new BulletInfo(this.battery.position, 1250, angle, '1', bulletManager.testID)
        bullet.getComponent(BulletCtr).initBullet(bulletInfo)
        colliderObjManager.setBulletCollider(bullet)
        bulletManager.testID += 1
    }

    create_fish() {

    }
    // update (dt) {}
    lateUpdate() {
        if (colliderObjManager.all_bullet_collider.size > 0) {
            colliderObjManager.check()
        }
    }

}
